package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingPlanPageDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProject;
import com.zzyl.entity.ProjectPlans;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.CountVo;
import com.zzyl.vo.NursingPlanProjectVo;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    NursingPlanMapper nursingPlanMapper;

    @Override
    //查询所有没有分页
    public List<NursingPlanVo> findAll() {
        List<NursingPlanVo> nursingPlanVo = nursingPlanMapper.findAll();
        //转vo


        return nursingPlanVo;
    }

    @Override
    //新增
    @Transactional
    public void insert(NursingPlanDto nursingPlanDto) {
        //先增加护理计划
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        //设置状态
        if(nursingPlan.getStatus()==null){
            nursingPlan.setStatus(0);
        }
        nursingPlanMapper.insert(nursingPlan);
        //再增加联表,注意主键返回
        //非空判断
        if(nursingPlanDto.getProjectPlans()!=null){
            List<ProjectPlans> projectPlansList = new ArrayList<>();
            nursingPlanDto.getProjectPlans().forEach(projectPlansDto -> {
                projectPlansDto.setPlanId(nursingPlan.getId());
                //转换为entry
                ProjectPlans projectPlans = new ProjectPlans();
                BeanUtils.copyProperties(projectPlansDto,projectPlans);
                projectPlansList.add(projectPlans);
            });
            nursingPlanMapper.insertProjectPlans(projectPlansList);
        }


    }

    @Override
    public PageResponse findPage(NursingPlanPageDto nursingPlanPageDto) {
        PageHelper.startPage(nursingPlanPageDto.getPageNum(),nursingPlanPageDto.getPageSize());
        Page<NursingPlanVo>  page = nursingPlanMapper.findPage(nursingPlanPageDto);
        return PageResponse.of(page, NursingPlanVo.class);
    }

    @Override
    public NursingPlanProjectVo findById(Long id) {
        NursingPlanProjectVo nursingPlanProjectVo = nursingPlanMapper.findById(id);
        return nursingPlanProjectVo;
    }

    @Override
    @Transactional
    public void update(NursingPlanDto nursingPlanDto) {
        //先修改护理计划
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        nursingPlanMapper.update(nursingPlan);
        //修改完成后删除所有联表数据
        nursingPlanMapper.deleteProjectPlans(nursingPlanDto.getId());
        //判断是否有联表数据
        if(nursingPlanDto.getProjectPlans()!=null){
            List<ProjectPlans> projectPlansList = new ArrayList<>();
            nursingPlanDto.getProjectPlans().forEach(projectPlansDto -> {
                projectPlansDto.setPlanId(nursingPlanDto.getId());
                //转换为entry
                ProjectPlans projectPlans = new ProjectPlans();
                BeanUtils.copyProperties(projectPlansDto,projectPlans);
                projectPlansList.add(projectPlans);
            });
            //新增
            nursingPlanMapper.insertProjectPlans(projectPlansList);
        }
    }

    @Override
    @Transactional
    public void deleteById(Long id) {
        //先删计划表
        nursingPlanMapper.deleteById(id);
        //再删除联表
        nursingPlanMapper.deleteProjectPlans(id);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        nursingPlanMapper.updateStatus(id,status);
    }


}
